package pusar_topic_unsubscribe

import (
	"github.com/apache/pulsar-client-go/pulsar"
	"go_19/initialize"
)

type TopicUnsubscribeStruct struct {
	Topic            initialize.TopicConsumer `json:"topic"`
	UnSubscribeNames []string                 `json:"unsubscribe_names"`
}

func TopicUnsubscribe(unsubscribeStruct TopicUnsubscribeStruct) {

	client := initialize.PulsarClient(unsubscribeStruct.Topic.ClientOption)
	for _, SubscriptionName := range unsubscribeStruct.UnSubscribeNames {
		consumer, err := client.Subscribe(pulsar.ConsumerOptions{
			Topic:            unsubscribeStruct.Topic.Topic,
			SubscriptionName: SubscriptionName,
			Type:             0,
		})
		if err == nil {
			consumer.Unsubscribe()
			consumer.Close()
		}
	}
	defer func() {
		client.Close()
	}()
}
